"""
 encoding    : utf-8
 @File       : basepage.py
 @Author     : Rao
 @Version    : 1.0
 @Description: 所有页面的基类，封装了常用的操作，打开页面、等待页面元素出现等
 @Date       : 2024/06/15
"""
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from common.OA_tools import *


class BasePage:
    def __init__(self):
        self.tools = Tools()
        self.driver = None
        self.data = None

    def open_page(self, data):
        self.tools.open_page(data)
        self.driver = self.tools.driver

    def get_screenshot(self, path):
        """根据传入的路径添加截图"""
        self.driver.save_screenshot(path)

    def get_popup_text(self):
        """获取弹窗文本"""
        return self.driver.switch_to.alert.text

    def close_driver(self):
        """关闭当前页面"""
        self.driver.close()

    def quit_driver(self):
        """关闭页面"""
        self.driver.quit()

    def refresh_driver(self):
        """刷新页面"""
        self.driver.refresh()

    def back_frame(self):
        """返回上一级的frame"""
        self.driver.switch_to.parent_frame()

    def back_frame_root(self):
        """返回最初的frame"""
        self.driver.switch_to.default_content()

    def get_current_title(self):
        """获取当前页面标题"""
        return self.driver.title

    def switch_to_main_frame(self):
        """切换到主iframe"""
        main_frame = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(
                (
                    By.ID,
                    "iframe_main"
                )
            )
        )
        self.driver.switch_to.frame(main_frame)

    def click_refresh(self):
        """点击刷新按钮"""
        self.driver.find_element(
            By.XPATH,
            "//*[@id='div_navbutton']/div[5]/button[3]"
        ).click()


# if __name__ == '__main__':
#     base_page = BasePage()
#     base_page.open_page(2, 1)
